<?php
$db = Database::getDatabase();

$ViewEvent;
$ViewTimeMode;
$ViewDay = null;
$ViewMonth = null;
$ViewYear = null;

// Fetch the available events.
$EventIDs = $EventNames = $Temp = array();
$Query = 'SELECT Type_ID, Type_Name FROM Event_Type';
$Result = $db->query($Query);
if ($db->hasRows($Result)) {
//    var_dump($db->GetRows($Result));
    $EventCount = $db->numRows($Result);
    for ($i = 0; $i < $EventCount; $i++) {
        $Temp = $db->getRow();
        $EventIDs[] = $Temp['Type_ID'];
        $EventNames[] = $Temp['Type_Name'];
    }
} else {
    echo 'No event types were returned. This is probably an improperly initialized database.';
    exit;
}

// Fill out our values if the form was submitted.
if (isset($_POST['SubmitButton']) && $_POST['SubmitButton']) {
    $ViewEvent = SanitizationArray($_POST['ViewEvent'], $EventIDs, $EventNames[0]);
    $ViewOutput = SanitizationArray($_POST['ViewOutput'], array('both', 'graph', 'table'), 'both');
    $ViewTimeMode = SanitizationArray($_POST['ViewTimeMode'], array('day', 'week', 'month', 'year'), 'week');

    $ViewDay = SanitizationArray($_POST['ViewDay'], range(1, 31), date("j"));
    $ViewMonth = SanitizationArray($_POST['ViewMonth'], range(1, 12), date("n"));
    $ViewYear = SanitizationArray($_POST['ViewYear'], range(2010, 2012), date("Y"));

    $ChartGETString = "?ViewEvent=$ViewEvent&ViewTimeMode=$ViewTimeMode&ViewDay=$ViewDay&ViewMonth=$ViewMonth&ViewYear=$ViewYear";

    $Query = "SELECT FROM";
}
?>
            <div style="margin: 20px">
                <h2>View Aggregate Statistics</h2>
                <span>This page will allow you to view aggregated data, such as hits, events and referrals over a period of time.</span><br />
                <br />
                <div id="statspanel">
                    <h4>Statistic View</h4>
                    <form action="./viewstats.php?view=aggregate" method="post">
                        <label for="ViewEvent">Event: </label>
                        <select id="ViewEvent" name="ViewEvent"><?php echo GenerateSelect($EventIDs, $EventNames); ?></select><br />
                        <label for="ViewOutput">Output as: </label>
                        <select id="ViewOutput" name="ViewOutput"><option value="both">Table and Graph</option><option value="graph">Graph Only</option><option value="table">Table Only</option></select><br />
                        <br />
                        <h4>Time View</h4>
                        <label for="ViewTimeMode">Time Period: </label>
                        <select id="ViewTimeMode" name="ViewTimeMode"><option value="day">Day</option><option selected="selected" value="week">Week</option><option value="month">Month</option><option value="year">Year</option></select><br />
                        <span>Starting date: </span>
                        <select id="ViewDay" name="ViewDay"><?php echo GenerateSelect(range(1, 31), range(1, 31), (!is_null($ViewDay) ? ($ViewDay - 1) : (date("j") - 1))); ?></select>
                        <select id="ViewMonth" name="ViewMonth"><?php echo GenerateMonthSelect($ViewMonth); ?></select>
                        <select id="ViewYear" name="ViewYear"><?php echo GenerateSelect(range(2010, 2012), range(2010, 2012), 1); ?></select>
                        <br />
                        <input type="submit" name="SubmitButton" value="View" />
                        <input type="reset" name="ResetButton" value="Reset" />
                    </form>
                </div>
                <?php
                if (isset($_POST['SubmitButton']) && $_POST['SubmitButton']) {
                    if ($ViewOutput == 'graph' || $ViewOutput == 'both') {
                        echo "<img src=\"chart.php$ChartGETString\" height=\"300\" width=\"600\" alt=\"chat\" title=\"chart\"/>";
                    }
                }
                ?>


            </div>